#hashlib提供了常见的摘要算法,即哈希算法
#计算出一个字符串的MD5值
import hashlib
md5 = hashlib.md5()
md5.update('how to use md5 in python hashlib?'.encode('utf-8'))
print(md5.hexdigest())

#如果数据量很大，可以分块多次调用update()
md5 = hashlib.md5()
md5.update('how to use md5 in '.encode('utf-8'))
md5.update('python hashlib?'.encode('utf-8'))
print(md5.hexdigest())

#改动字母
sha1 = hashlib.sha1()
sha1.update('how to use sha1 in '.encode('utf-8'))
sha1.update('python hashlib?'.encode('utf-8'))
print(sha1.hexdigest())

#练习
import hashlib
def calc_md5(password):
    if isinstance(password,str):
        s = password.encode('utf-8')
    md5 = hashlib.md5()
    md5.update(s)
    return md5.hexdigest()

def login(user, password):
    if db.get(user,None) == calc_md5(password):
         return True
    return False